package org.abettor.pushbox.db;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.abettor.pushbox.model.PushBoxMapBean;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 * 对PushBoxMapBean管理进行封装的类
 * 
 * @author jzh
 * 
 */
public class PushBoxMapBeanManage {
	private Context context;

	public PushBoxMapBeanManage(Context context) {
		this.context = context;
	}

	/**
	 * 向数据库中插入记录的操作
	 */
	public void insertPushBoxMapBean(List<PushBoxMapBean> list) {
		PushBoxDbHelper helper = new PushBoxDbHelper(context);
		SQLiteDatabase base = helper.getWritableDatabase();
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		for (PushBoxMapBean bean : list) {
			ContentValues cv = new ContentValues();
			cv.put("name", bean.getName());
			cv.put("type", bean.getType());
			cv.put(PushBoxDbHelper.PUSH_BOX_SOLVED, bean.getIssolved());
			cv.put("map_filename", bean.getMapFileName());
			cv.put("create_time", dateFormat.format(new Date()));
			cv.put("isdraft", bean.getIsdraft());
			cv.put("missionid", bean.getMissionid());
			base.insert(PushBoxDbHelper.PUSH_BOX_TABLE_NAME, null, cv);
		}
		base.close();
	}

	/**
	 * 插入单一的记录的操作
	 * 
	 * @param bean
	 * @return
	 */
	public int insertSinglePushBoxMapBean(PushBoxMapBean bean) {
		PushBoxDbHelper helper = new PushBoxDbHelper(context);
		SQLiteDatabase base = helper.getWritableDatabase();
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		ContentValues cv = new ContentValues();
		cv.put("name", bean.getName());
		cv.put("type", bean.getType());
		cv.put("map_filename", bean.getMapFileName());
		cv.put("create_time", dateFormat.format(new Date()));
		cv.put("isdraft", bean.getIsdraft());
		int ret = (int) base.insert(PushBoxDbHelper.PUSH_BOX_TABLE_NAME, null,
				cv);
		base.close();
		return ret;
	}

	/**
	 * 更新记录
	 * 
	 * @param bean
	 * @return
	 */
	public void updateSinglePushBoxMapBean(PushBoxMapBean bean) {
		PushBoxDbHelper helper = new PushBoxDbHelper(context);
		SQLiteDatabase base = helper.getWritableDatabase();
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		ContentValues cv = new ContentValues();
		cv.put("name", bean.getName());
		cv.put("type", bean.getType());
		cv.put("map_filename", bean.getMapFileName());
		cv.put("create_time", dateFormat.format(new Date()));
		cv.put("isdraft", bean.getIsdraft());
		base.update(PushBoxDbHelper.PUSH_BOX_TABLE_NAME, cv,
				PushBoxDbHelper.PUSH_BOX_ID + "=" + bean.getId(), null);
		base.close();
	}

	/**
	 * 更新地图上传的状态
	 * 
	 * @param roteId
	 * @param state
	 */
	public void updateUploadStateSuccess(int mapId, int roteId) {
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		PushBoxDbHelper helper = new PushBoxDbHelper(context);
		SQLiteDatabase base = helper.getWritableDatabase();
		ContentValues cv = new ContentValues();
		cv.put(PushBoxDbHelper.PUSH_BOX_REMOTE_ID, roteId);
		cv.put(PushBoxDbHelper.PUSH_BOX_HASUPLOADED, 1);
		cv.put(PushBoxDbHelper.PUSH_BOX_UPLOADED_TIME,
				dateFormat.format(new Date()));
		base.update(PushBoxDbHelper.PUSH_BOX_TABLE_NAME, cv,
				PushBoxDbHelper.PUSH_BOX_ID + "=" + mapId, null);
		base.close();
	}

	/**
	 * 获得相应记录的文件名字
	 * 
	 * @param id
	 * @return
	 */
	public String getFileName(int id) {
		String name = null;
		PushBoxDbHelper helper = new PushBoxDbHelper(context);
		SQLiteDatabase base = helper.getWritableDatabase();
		Cursor c = base.query(PushBoxDbHelper.PUSH_BOX_TABLE_NAME,
				new String[] { PushBoxDbHelper.PUSH_BOX_MAP_FILENAME },
				PushBoxDbHelper.PUSH_BOX_ID + "=" + id, null, null, null, null);
		if (c.moveToFirst()) {
			int index = c.getColumnIndex(PushBoxDbHelper.PUSH_BOX_MAP_FILENAME);
			name = c.getString(index);// 获得名字
		}
		c.close();
		base.close();
		return name;
	}

	/**
	 * 获得相应记录的文件名字
	 * 
	 * @param id
	 * @return
	 */
	public int getMissionId(int id) {
		int missionId = 0;
		PushBoxDbHelper helper = new PushBoxDbHelper(context);
		SQLiteDatabase base = helper.getWritableDatabase();
		Cursor c = base.query(PushBoxDbHelper.PUSH_BOX_TABLE_NAME,
				new String[] { PushBoxDbHelper.PUSH_BOX_MISSION_ID },
				PushBoxDbHelper.PUSH_BOX_ID + "=" + id, null, null, null, null);
		if (c.moveToFirst()) {
			int index = c.getColumnIndex(PushBoxDbHelper.PUSH_BOX_MISSION_ID);
			missionId = c.getInt(index);// 获得名字
		}
		c.close();
		base.close();
		return missionId;
	}

	/**
	 * 判断是否上传过的状态
	 * 
	 * @param id
	 * @return
	 */
	public int getUploadedStatus(int id) {
		int uploaded = 0;
		PushBoxDbHelper helper = new PushBoxDbHelper(context);
		SQLiteDatabase base = helper.getWritableDatabase();
		Cursor c = base.query(PushBoxDbHelper.PUSH_BOX_TABLE_NAME,
				new String[] { PushBoxDbHelper.PUSH_BOX_HASUPLOADED },
				PushBoxDbHelper.PUSH_BOX_ID + "=" + id, null, null, null, null);
		if (c.moveToFirst()) {
			int index = c.getColumnIndex(PushBoxDbHelper.PUSH_BOX_HASUPLOADED);
			uploaded = c.getInt(index);// 获得名字
		}
		c.close();
		base.close();
		return uploaded;
	}

	/**
	 * 获得关卡的状态
	 * 
	 * @param id
	 * @return
	 */
	public int getMissionStatus(int id) {
		int status = 0;
		PushBoxDbHelper helper = new PushBoxDbHelper(context);
		SQLiteDatabase base = helper.getWritableDatabase();
		Cursor c = base.query(PushBoxDbHelper.PUSH_BOX_TABLE_NAME,
				new String[] { PushBoxDbHelper.PUSH_BOX_SOLVED },
				PushBoxDbHelper.PUSH_BOX_ID + "=" + id, null, null, null, null);
		if (c.moveToFirst()) {
			int index = c.getColumnIndex(PushBoxDbHelper.PUSH_BOX_SOLVED);
			status = c.getInt(index);// 获得名字
		}
		c.close();
		base.close();
		return status;
	}

	/**
	 * 根据关卡名字获得id
	 * 
	 * @param missionId
	 * @return
	 */
	public int getIdByMission(int missionId) {
		int id = 0;
		PushBoxDbHelper helper = new PushBoxDbHelper(context);
		SQLiteDatabase base = helper.getWritableDatabase();
		Cursor c = base.query(PushBoxDbHelper.PUSH_BOX_TABLE_NAME,
				new String[] { PushBoxDbHelper.PUSH_BOX_ID },
				PushBoxDbHelper.PUSH_BOX_MISSION_ID + "=" + missionId + " and "
						+ PushBoxDbHelper.PUSH_BOX_TYPE + "="
						+ PushBoxMapBean.CLASSIC_TYPE, null, null, null, null);
		if (c.moveToFirst()) {
			int index = c.getColumnIndex(PushBoxDbHelper.PUSH_BOX_ID);
			id = c.getInt(index);// 获得名字
		}
		c.close();
		base.close();
		return id;
	}

	/**
	 * 获得经典地图我通过的关卡数
	 * 
	 * @return
	 */
	public int getClassicPassCount() {
		int count = 0;
		PushBoxDbHelper helper = new PushBoxDbHelper(context);
		SQLiteDatabase base = helper.getWritableDatabase();
		Cursor c = base.query(PushBoxDbHelper.PUSH_BOX_TABLE_NAME,
				new String[] { "count(" + PushBoxDbHelper.PUSH_BOX_ID
						+ ") as cut" }, PushBoxDbHelper.PUSH_BOX_TYPE + "="
						+ PushBoxMapBean.CLASSIC_TYPE + " and "
						+ PushBoxDbHelper.PUSH_BOX_SOLVED + "=1", null, null,
				null, null);
		if (c.moveToFirst()) {
			int index = c.getColumnIndex("cut");
			count = c.getInt(index);// 获得名字
		}
		c.close();
		base.close();
		return count;
	}
	/**
	 * 获得自创地图的个数
	 * @return
	 */
	public int getMyCreateMapCount(){
		int count = 0;
		PushBoxDbHelper helper = new PushBoxDbHelper(context);
		SQLiteDatabase base = helper.getWritableDatabase();
		Cursor c = base.query(PushBoxDbHelper.PUSH_BOX_TABLE_NAME,
				new String[] { "count(" + PushBoxDbHelper.PUSH_BOX_ID
						+ ") as cut" }, PushBoxDbHelper.PUSH_BOX_TYPE + "="
						+ PushBoxMapBean.SELFMAKE_TYPE, null, null,
				null, null);
		if (c.moveToFirst()) {
			int index = c.getColumnIndex("cut");
			count = c.getInt(index);// 获得名字
		}
		c.close();
		base.close();
		return count;
	}
	/**
	 * 删除地图的操作
	 * 
	 * @param id
	 */
	public void delPushBoxMapBean(int id) {
		PushBoxDbHelper helper = new PushBoxDbHelper(context);
		SQLiteDatabase base = helper.getWritableDatabase();
		base.delete(PushBoxDbHelper.PUSH_BOX_TABLE_NAME,
				PushBoxDbHelper.PUSH_BOX_ID + "=" + id, null);
		base.close();
	}

	/**
	 * 更新地图的状态
	 * 
	 * @param id
	 */
	public void updatePushBoxMapBeanSuccess(int id) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		PushBoxDbHelper helper = new PushBoxDbHelper(context);
		SQLiteDatabase base = helper.getWritableDatabase();
		ContentValues cv = new ContentValues();
		cv.put(PushBoxDbHelper.PUSH_BOX_RESOLVE_TIME, sdf.format(new Date()));
		cv.put(PushBoxDbHelper.PUSH_BOX_SOLVED, "1");
		base.update(PushBoxDbHelper.PUSH_BOX_TABLE_NAME, cv,
				PushBoxDbHelper.PUSH_BOX_ID + "=" + id, null);
		base.close();
	}

	/**
	 * 更新地图的状态为可以继续做
	 * 
	 * @param id
	 */
	public void updatePushBoxMapBeanToPush(int id) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		PushBoxDbHelper helper = new PushBoxDbHelper(context);
		SQLiteDatabase base = helper.getWritableDatabase();
		ContentValues cv = new ContentValues();
		cv.put(PushBoxDbHelper.PUSH_BOX_RESOLVE_TIME, sdf.format(new Date()));
		cv.put(PushBoxDbHelper.PUSH_BOX_SOLVED, "2");
		base.update(PushBoxDbHelper.PUSH_BOX_TABLE_NAME, cv,
				PushBoxDbHelper.PUSH_BOX_ID + "=" + id, null);
		base.close();
	}

	/**
	 * 向数据库中增加或者修改记录
	 * 
	 * @param bean
	 */
	public int insertOrUpdateBoxMapBean(PushBoxMapBean bean) {
		int _id = bean.getId();
		if (_id == -1) {// 增加记录
			return insertSinglePushBoxMapBean(bean);
		} else {
			updateSinglePushBoxMapBean(bean);
			return _id;
		}
	}
}
